package com.eoiiioe.model.two;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import com.eoiiioe.model.two.utils.StringUtils;

public class ZhbRegisterActivity extends Activity implements OnClickListener {

    private EditText et_reg_phone_num;
    private EditText et_reg_vercode;
    private Button bt_reg_next;
    private Button get_vercode;
    private Handler mHandler;
    private String reg_phone;
    private static int RESULT = 0x00001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_register);

        SMSSDK.initSDK(this, "68108eaf62ce", "8fd56a219d4ab99ec35a648b72a0a375");

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int event = msg.arg1;
                int result = msg.arg2;
                Object data = msg.obj;
                Log.e("event", "event=" + event);
                if (result == RESULT) {
                    if (data.toString().equalsIgnoreCase("0")) {
                        get_vercode.setText("获取验证码");
                        get_vercode.setClickable(true);
                        get_vercode.setFocusable(true);
                        // get_vercode.setBackgroundResource(R.drawable.btn_silver);
                    } else {
                        get_vercode.setText("(" + data + "秒)");
                    }
                } else if (result == SMSSDK.RESULT_COMPLETE) {

                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
                        Toast.makeText(getApplicationContext(), "验证成功", Toast.LENGTH_SHORT).show();
                        ZhbRegisterActivity.this.finish();
                        startActivity(new Intent(ZhbRegisterActivity.this, MainActivity.class));

                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {// 返回支持发送验证码的国家列表
                        // Toast.makeText(getApplicationContext(), "获取国家列表成功", Toast.LENGTH_SHORT).show();
                        // countryTextView.setText(data.toString());
                    }
                } else {
                    ((Throwable) data).printStackTrace();
                    // int resId = getStringRes(ZhbRegisterActivity.this, "smssdk_network_error");
                    Toast.makeText(ZhbRegisterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                    // if (resId > 0) {
                    // Toast.makeText(ZhbRegisterActivity.this, resId, Toast.LENGTH_SHORT).show();
                    // }
                }
            }
        };

        EventHandler eh = new EventHandler() {

            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                mHandler.sendMessage(msg);
            }

        };
        SMSSDK.registerEventHandler(eh);

        initView();

    }

    private void initView() {
        et_reg_phone_num = (EditText) findViewById(R.id.et_reg_phone_num);
        et_reg_vercode = (EditText) findViewById(R.id.et_reg_vercode);

        get_vercode = (Button) findViewById(R.id.bt_reg_get_vercode);
        bt_reg_next = (Button) findViewById(R.id.bt_reg_next);

        get_vercode.setOnClickListener(this);
        bt_reg_next.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_reg_get_vercode:// 获取验证码
            reg_phone = et_reg_phone_num.getText().toString().trim();
            if (!StringUtils.isEmpty(reg_phone)) {

                SMSSDK.getVerificationCode("86", reg_phone);

                get_vercode.setClickable(false);
                get_vercode.setFocusable(false);
                // get_vercode.setBackgroundResource(R.drawable.btn_subsub_bg);
                new Thread() {
                    int a = 30;

                    public void run() {
                        while (a > 0) {
                            try {
                                sleep(1000);
                                a--;

                                Message msg = new Message();
                                msg.arg2 = RESULT;
                                msg.obj = a;
                                mHandler.sendMessage(msg);

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                }.start();

            } else {
                Toast.makeText(this, "电话不能为空", 1).show();
            }
            break;
        case R.id.bt_reg_next:
            String vercodeString = et_reg_vercode.getText().toString().trim();
            if (!TextUtils.isEmpty(vercodeString)) {
                SMSSDK.submitVerificationCode("86", reg_phone, vercodeString);
            } else {
                Toast.makeText(this, "验证码不能为空", 1).show();
            }
            break;

        default:
            break;
        }

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }

}
